package org.eae.eldf.webapp.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.vo.PageListVo;
import org.eae.eldf.entity.base.BaseCompanyLinkman;
import org.eae.eldf.framework.annotation.Token;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.service.base.BaseCompanyLinkmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/base")
public class BaseCompanyLinkmanController extends AbstractController<BaseCompanyLinkman> {
	private BaseCompanyLinkmanService baseCompanyLinkmanService;

	@Autowired
	public void setBaseCompanyLinkmanService(BaseCompanyLinkmanService baseCompanyLinkmanService) {
		this.baseCompanyLinkmanService = baseCompanyLinkmanService;
	}

	/**
	 * 分页查询联系人信息
	 * @param searchValue
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/linkman/list")
	public String initPage(@RequestParam(required = false) String searchValue, ModelMap model,
			HttpServletRequest request, HttpServletResponse response) {
		return "/base/linkman/list";
	}
	@RequestMapping(value = "/linkman/list_json")
	public @ResponseBody
	PageListVo<BaseCompanyLinkman> pageList(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer rows,
			@RequestParam(required = false) String searchValue) {
		Pagination<BaseCompanyLinkman> pager = super.initPagination(request);
		pager = baseCompanyLinkmanService.pageLinkmanList(pager, searchValue);
		return pageListJson(pager);
	}

	/**
	 * 新增企业联系人信息
	 * @param request
	 * @param response
	 * @param roleName
	 * @param roleRemark
	 * @return
	 */
	@RequestMapping(value = "/linkman/add")
	public @ResponseBody
	String linkmanAdd(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute BaseCompanyLinkman baseCompanyLinkman) {
		try {
			baseCompanyLinkmanService.insertBaseCompanyLinkman(baseCompanyLinkman);
		} catch (Exception e) {
			return "error";
		}
		return "success";
	}

	/**
	 * 编辑联系人信息
	 * @param request
	 * @param response
	 * @param baseCompanyLinkmanId
	 * @param baseCompanyLinkman
	 * @return
	 */
	@RequestMapping(value = "/linkman/edit")
	public @ResponseBody
	String linkmanEdit(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute BaseCompanyLinkman baseCompanyLinkman) {
		try {
			baseCompanyLinkmanService.updateBaseCompanyLinkman(baseCompanyLinkman);
		} catch (Exception e) {
			return "error";
		}
		return "success";
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/linkman/delete")
	public @ResponseBody
	String delete(@RequestParam Long id) {
		try {
			baseCompanyLinkmanService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	/**
	 * 页面跳转
	 * @param id
	 * @param model
	 * @param searchValue
	 * @param sorting_column_order
	 * @param sorting_asc_desc
	 * @param iDisplayStart
	 * @return
	 */
	@RequestMapping("/linkman/form")
	@Token(save = true)
	public String toFormPage(@RequestParam(required = false) Long id, ModelMap model,
			@RequestParam(required = false) String searchValue,
			@RequestParam(required = false) String sorting_column_order,
			@RequestParam(required = false) String sorting_asc_desc,
			@RequestParam(required = false) String iDisplayStart) {
		BaseCompanyLinkman baseCompanyLinkman = null;
		if (null != id) {
			baseCompanyLinkman = baseCompanyLinkmanService.findById(id);
		} else {
			baseCompanyLinkman = new BaseCompanyLinkman();
		}

		model.addAttribute("baseCompanyLinkman", baseCompanyLinkman);
		model.addAttribute("searchValue", searchValue);
		model.addAttribute("sorting_column_order", sorting_column_order);
		model.addAttribute("sorting_asc_desc", sorting_asc_desc);
		model.addAttribute("iDisplayStart", iDisplayStart);
		return "/base/linkman/form";
	}

}
